package io.renren.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.entity.CarTypeEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author zyzhao
 * @date 2022年04月14日 23:29
 */

@Component
public class TransUtils {
    
    @Autowired
    private RedisOperator redisOp;
    @Autowired
    private TransUtilsDao transUt;
    
    private static RedisOperator redisOperator;
    
    private static TransUtilsDao transUtilsDao;
    
    @PostConstruct
    public void init(){
        redisOperator = redisOp;
        transUtilsDao = transUt;
    }
    
    private static final String CAR_TYPE_KEY = "CAR_TYPE";
    private static final String REPAIR_TYPE_KEY = "REPAIR_TYPE";
    
    public static String getCarTypeTrans(String carType){
        String typeTrans = redisOperator.hget(CAR_TYPE_KEY, carType);
        // 没有对应值则更新Redis
        if (StringUtils.isEmpty(typeTrans)){
            typeTrans = transUtilsDao.selectById(carType).getName();
            redisOperator.hset(CAR_TYPE_KEY, carType, typeTrans);
        }
        
        return typeTrans;
    }
    
    public static String getRepairTypeTrans(String carType){
        String typeTrans = redisOperator.hget(REPAIR_TYPE_KEY, carType);
        // 没有对应值则更新Redis
        if (StringUtils.isEmpty(typeTrans)){
            typeTrans = transUtilsDao.selectRepairTrnas(carType);
            redisOperator.hset(REPAIR_TYPE_KEY, carType, typeTrans);
        }
        
        return typeTrans;
    }
    
    /**
     * 根据父级标签id获取对应汽车相关标签列表
     * @author zyzhao
     * @date 2022/4/19 23:01
     * @param pid 父级标签id
     * @return java.util.List<io.renren.common.entity.CarTypeEntity>
     */
    public static List<CarTypeEntity> getCarTypeDictByPid(String pid){
        QueryWrapper<CarTypeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        return transUtilsDao.selectList(queryWrapper);
    }
    
    /**
     * 根据父级标签id获取维修相关下级标签列表
     * @author zyzhao
     * @date 2022/4/19 23:00
     * @param pid 父级标签id
     * @return java.util.List<io.renren.common.entity.CarTypeEntity>
     */
    public static List<CarTypeEntity> getRepairTypeDictByPid(String pid){
        return transUtilsDao.selectRepairList(pid);
    }
    
    //@Autowired
    //public void setTransUtilsDao(TransUtilsDao transUtilsDao) {
    //    TransUtils.transUtilsDao = transUtilsDao;
    //}
    
    //@Autowired
    //public void setRedisOperator(RedisOperator redisOperator) {
    //    TransUtils.redisOperator = redisOperator;
    //}
}
